package com.td.framework.webmanager.dlyh.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.td.framework.common.AbstractAction;
import com.td.framework.common.Constant;
import com.td.framework.webmanager.dlyh.service.DlyhService;
import com.td.framework.webmanager.domain.AppsysDlyh;
import com.td.framework.webmanager.domain.AppsysDlyhBean;
import com.td.framework.webmanager.domain.AppsysQxfwjlgx;
import com.td.framework.webmanager.domain.AppsysQxygns;
import com.td.framework.webmanager.domain.AppsysRyjbxx;
import com.td.framework.webmanager.domain.AppsysYhz;
import com.td.framework.webmanager.domain.AppsysYmgnBean;
import com.td.framework.webmanager.domain.AppsysYmxzfwjlBean;
import com.td.framework.webmanager.domain.YmxztjBean;
//import com.td.framework.webmanager.log.service.DlrzService;
import com.td.framework.webmanager.login.UserInfo;
import com.td.framework.webmanager.page.PageService;
import com.td.framework.webmanager.qxgl.service.QxglService;
import com.td.framework.webmanager.qxygns.service.QxygnsService;
import com.td.framework.webmanager.ryjbxx.service.RyjbxxService;
import com.td.framework.webmanager.yhz.service.YhzService;
import com.opensymphony.xwork2.Action;

/**
 * 
 * Title:登录用户处理Action Description: 处理登录用户信息 
 * Copyright: Copyright (c) 2008
 * Company 北京水软科技有限公司
 * @author
 * @version 1.0
 */
public class DlyhAction extends AbstractAction implements Action {
	private static final long serialVersionUID = -4101483281074643448L;
	private DlyhService dlyhService = null;// 登录用户业务层接口;
	private RyjbxxService ryjbxxService = null;// 人员信息业务层接口;
	private List<AppsysDlyhBean> dlyhList = null;// 用户List;
	private List<AppsysRyjbxx> ryjbxxList = null;// 人员基本信息List;
	private AppsysDlyh appsysDlyh = null;// 用户信息
	private AppsysDlyhBean dlyhBean = null;// 列表显示对象;
	private String yhbhs = "";// 用户编号组;
	private String yhbh = "";// 用户编号;
	private String target = "";// 返回参数;
	private QxglService qxglService = null;// 权限处理业务层接口;
	private String sysTreeXml = "";// 系统权限树xml字符流;
	private String yhzTreeXml = "";// 用户当前系统权限树xml字符流;
	private String[] ymbhs = null;// 页面编号,授权时用;
	private String urlFlag = "";// 区分字段,授权是区分是点击下一步还是完成;
	private List<AppsysQxygns> rightTreeList = null;// 用户组当前权限List;
	private List<AppsysYmgnBean> appsysYmgnBeanList = null;// 页面功能List;
	private List<AppsysYmgnBean> xzzdList = null;// 限制字段List;
	private List<AppsysYmxzfwjlBean> fwjlList = null;// 限制访问记录List;
	private List<AppsysQxfwjlgx> qxfwjlgxs = null;// 权限与限制访问记录关系数组;
	private List<YmxztjBean> ymxztjList = null;// 限制条件List;
	private String xzbh = "";// 限制编号,选择条件用;
	private Long qxbh = null;// 权限编号;
	private String[] gnbhs = null;// 功能编号与权限标志组合数组;
	private YhzService yhzService = null;// 用户组业务层接口;
	private List<AppsysYhz> yhzList = null;// 用户组list;
	private List<AppsysYhz> currYhzList = null;// 用户当前所属用户组list;
	private String yhzbhs = "";// 所选用户组编号;
	private String ryxm = "";// 人员姓名
	private Timestamp timeStamp = null;
	private QxygnsService qxygnsService;//功能权限接口
	private List<AppsysQxygns> mainFunc;// 生成功能树List; 
	
	/**
	 * @toDo:查询用户信息列表;
	 * @return target;
	 */
	public String listDlyh() {
		int rows = 0;
		dlyhService.setSql(dlyhBean, this.getUserInfo().getYhbh(),
				Constant.SYS_XTBM, this.getUserInfo().getBmbh());
		rows = dlyhService.getRows();
		page = PageService.getPage(rows, currentPage, pageSize, pageMethod);
		this.setCurrentPage(page.getCurentPage());
		this.setPageSize(page.getPageSize());
		dlyhList = dlyhService.getListPageData(dlyhService.getSql(), page
				.getPageSize(), page.getStartRow());
		target = "list";
		return target;
	}

	/**
	 * @toDo:创建用户信息Form;
	 * @return target;
	 */
	public String createDlyhForm() {
		appsysDlyh = null;
		target = "createForm";
		return target;
	}

	/**
	 * @toDo:保存用户信息;
	 * @return target;
	 */
	public String createDlyh() {
		if (dlyhService.addDlyh(appsysDlyh)) {
			this.handleMessage("info", "添加成功,请继续操作!");
			appsysDlyh = null;
			target = "addSuccess";
		} else {
			this.handleMessage("info", "添加失败,您添加的信息不符合要求,请重新添加!");
			target = "addFailure";
		}
		return target;
	}

	/**
	 * @toDo:创建用户信息修改Form;
	 * @return target;
	 */
	public String reworkDlyhForm() {
		appsysDlyh = dlyhService.getDlyh(yhbhs);
		// ryjbxxService.setSql("",null,this.getUserInfo().getYhbh(),this.getUserInfo().getBmbh(),Constant.SYS_XTBM);
		if (appsysDlyh != null) {
			ryxm = ryjbxxService.getAppsysRyjbxx(appsysDlyh.getRybh()).getXm();
			target = "reworkForm";
		} else {
			target = "list";
		}
		return target;
	}

	/**
	 * @toDo: 更新用户信息;
	 * @return target;
	 */
	public String reworkDlyh() {
		if (dlyhService.updateDlyh(appsysDlyh, 0)) {
			result="OK";
			target = "updateSuccess";			
			appsysDlyh = null;
		} else {
			target = "updateFailure";
			this.handleMessage("info", "修改失败,请重新修改!");
		}
		return target;
	}

	/**
	 * @toDo:删除用户信息;
	 * @return target;
	 */
	public String removeDlyh() {
		dlyhService.deleteDlyh(yhbh);
		target = "deleteSuccess";
		return target;
	}

	/**
	 * @toDo:用户登录;
	 * @return target;
	 */
	public String loginYh() {
		UserInfo userInfo = dlyhService.loadUserInfo(dlyhBean, urlFlag);
		dlyhBean = null;
		String yz = "";
		 //验证使用
		// if("Y".equals(Constant.SYSTEM_SY)){//需要试用
		//yz = ResourceTools.initProperties();//处理license
		// }
		yz = "true";
		if ("true".equals(yz) || "forever".equals(yz)) {//验证license
			if (userInfo != null) {
				this.getHttpsession().setAttribute("userInfo", userInfo);
				getHttpsession().setAttribute(getHttpsession().getId(),
						userInfo);
				target = "success";
			} else {
				this.handleMessage("info", "用户名/密码不正确,请重新登录!");
				target = "failure";
			}
			//得到用户的首页大功能权限
			mainFunc = qxygnsService.getMainFunc(userInfo.getYhbh());
		} else {
			this.handleMessage("info", yz);
			target = "failure";
		}

		return target;
	}

	/**
	 * @toDo:修改密码Form;
	 * @return target;
	 */
	public String reworkMimaForm() {
		UserInfo userInfo = (UserInfo) getHttpsession()
				.getAttribute("userInfo");
		if (userInfo != null) {
			appsysDlyh = dlyhService.getDlyh(userInfo.getYhbh());
		} else {
			appsysDlyh = null;
		}
		return target = "mimaForm";
	}

	/**
	 * @toDo:修改密码
	 * @return target;
	 */
	public String reworkMima() {
		if (dlyhService.updateDlyh(appsysDlyh, 1)) {
			target = "updateSuccess";
			this.handleMessage("info", "密码修改成功!");
		} else {
			target = "updateFailure";
			this.handleMessage("info", "密码修改失败!");
		}
		return target;
	}

	/**
	 * @toDo:生成用户组当前权限树;
	 * @return target;
	 */
	public String createRightTree() {
		rightTreeList = qxglService.getRightTreeList(yhbhs, new Long(1), 2);
		qxbh = qxglService.getQxbh(yhbhs, new Long(1));
		target = "rightTree";
		return target;
	}

	/**
	 * toDo:选择条件
	 * 
	 * @return target;
	 */
	public String selCondition() {
		ymxztjList = qxglService.findYmxztjBean(qxbh, xzbh);
		target = "selCondition";
		return target;
	}

	/**
	 * @toDo:生成页面级功能页面
	 * @return target;
	 */
	@SuppressWarnings("unchecked")
	public String createYmgns() {
		appsysYmgnBeanList = new ArrayList();
		xzzdList = new ArrayList();
		if (qxbh != null && ymbhs != null) {
			appsysYmgnBeanList = qxglService.findAppsysYmgnBean(qxbh, ymbhs[0],
					1);
			xzzdList = qxglService.findAppsysYmgnBean(qxbh, ymbhs[0], 2);
			fwjlList = qxglService.findAppsysYmxzfwjlBean(qxbh, ymbhs[0]);
		}
		target = "ymgns";
		return target;
	}

	/**
	 * @toDo:用户组页面级功能授权保存;
	 * @return target;
	 */
	public String changeYmgnRight() {
		if (qxglService.addYmgnRight(qxbh + "", ymbhs[0], gnbhs, qxfwjlgxs)) {
			this.handleMessage("info", "授权成功,请继续操作!");
			target = "changeSuccess";
		} else {
			this.handleMessage("info", "授权失败,请重新操作!");
			target = "changeFailure";
		}
		appsysYmgnBeanList = qxglService.findAppsysYmgnBean(qxbh, ymbhs[0], 1);
		xzzdList = qxglService.findAppsysYmgnBean(qxbh, ymbhs[0], 2);
		fwjlList = qxglService.findAppsysYmxzfwjlBean(qxbh, ymbhs[0]);
		return target;
	}

	/**
	 * @toDo:选择用户组;
	 * @return target;
	 */
	public String selYhz() {
		yhzService.setSql(null);
		yhzList = yhzService.findByYhbh(yhbhs, 1);
		currYhzList = yhzService.findByYhbh(yhbhs, 0);
		target = "selYhz";
		return target;
	}

	/**
	 * @toDo:保存用户用户组关系数据;
	 * @return target;
	 */
	public String createSsYhz() {
		if (yhzService.addAppsysYhYhzGx(yhbhs, yhzbhs, 0)) {
			target = "addSuccess";
			this.handleMessage("info", "选择用户组成功!");
		} else {
			target = "addFailure";
			this.handleMessage("info", "选择用户组失败!");
		}
		this.handleMessage("isVisual", "1");
		appsysDlyh = null;
		yhzbhs = "";
		return target;
	}

	/**
	 * @toDo:还原密码;
	 * @return target;
	 */
	public String initMima() {
		if (dlyhService.initMima(yhbh)) {
			urlFlag = "initSuccess";
		} else {
			urlFlag = "initFailure";
		}
		target = "initPassword";
		return target;
	}

	/**
	 * @toDo:get登录用户Service接口
	 * @return the dlyhService
	 */
	public DlyhService getDlyhService() {
		return dlyhService;
	}

	/**
	 * @toDo:set登录用户Service接口
	 * @param dlyhService
	 *            the dlyhService to set
	 */
	public void setDlyhService(DlyhService dlyhService) {
		this.dlyhService = dlyhService;
	}

	/**
	 * @toDo:get用户List;
	 * @return the dlyhList
	 */
	public List<AppsysDlyhBean> getDlyhList() {
		return dlyhList;
	}

	/**
	 * @toDo:set用户List;
	 * @param dlyhList
	 *            the dlyhList to set
	 */
	public void setDlyhList(List<AppsysDlyhBean> dlyhList) {
		this.dlyhList = dlyhList;
	}

	/**
	 * @toDo:get人员信息Service接口;
	 * @return the ryjbxxService;
	 */
	public RyjbxxService getRyjbxxService() {
		return ryjbxxService;
	}

	/**
	 * @toDo:set人员信息Service接口;
	 * @param ryjbxxService
	 *            the ryjbxxService to set
	 */
	public void setRyjbxxService(RyjbxxService ryjbxxService) {
		this.ryjbxxService = ryjbxxService;
	}

	/**
	 * @toDo:get人员基本信息List;
	 * @return the ryjbxxList
	 */
	public List<AppsysRyjbxx> getRyjbxxList() {
		return ryjbxxList;
	}

	/**
	 * @toDo:set人员基本信息List;
	 * @param ryjbxxList
	 *            the ryjbxxList to set
	 */
	public void setRyjbxxList(List<AppsysRyjbxx> ryjbxxList) {
		this.ryjbxxList = ryjbxxList;
	}

	/**
	 * @toDo:get用户信息
	 * @return the appsysDlyh
	 */
	public AppsysDlyh getAppsysDlyh() {
		return appsysDlyh;
	}

	/**
	 * @toDo:set用户信息
	 * @param appsysDlyh
	 *            the appsysDlyh to set
	 */
	public void setAppsysDlyh(AppsysDlyh appsysDlyh) {
		this.appsysDlyh = appsysDlyh;
	}

	/**
	 * @toDo:get用户编号组;
	 * @return the yhbhs
	 */
	public String getYhbhs() {
		return yhbhs;
	}

	/**
	 * @toDo:set用户编号组;
	 * @param yhbhs
	 *            the yhbhs to set
	 */
	public void setYhbhs(String yhbhs) {
		this.yhbhs = yhbhs;
	}

	/**
	 * @toDo:get
	 * @return the dlyhBean
	 */
	public AppsysDlyhBean getDlyhBean() {
		return dlyhBean;
	}

	/**
	 * @toDo:set
	 * @param dlyhBean
	 *            the dlyhBean to set
	 */
	public void setDlyhBean(AppsysDlyhBean dlyhBean) {
		this.dlyhBean = dlyhBean;
	}

	/**
	 * @toDo:get
	 * @return the target
	 */
	public String getTarget() {
		return target;
	}

	/**
	 * @toDo:set
	 * @param target
	 *            the target to set
	 */
	public void setTarget(String target) {
		this.target = target;
	}
	/**
	 * @toDo:get
	 * @return the sysTreeXml
	 */
	public String getSysTreeXml() {
		return sysTreeXml;
	}

	/**
	 * @toDo:set
	 * @param sysTreeXml
	 *            the sysTreeXml to set
	 */
	public void setSysTreeXml(String sysTreeXml) {
		this.sysTreeXml = sysTreeXml;
	}

	/**
	 * @toDo:get
	 * @return the yhzTreeXml
	 */
	public String getYhzTreeXml() {
		return yhzTreeXml;
	}

	/**
	 * @toDo:set
	 * @param yhzTreeXml
	 *            the yhzTreeXml to set
	 */
	public void setYhzTreeXml(String yhzTreeXml) {
		this.yhzTreeXml = yhzTreeXml;
	}

	/**
	 * @toDo:get
	 * @return the ymbhs
	 */
	public String[] getYmbhs() {
		return ymbhs;
	}

	/**
	 * @toDo:set
	 * @param ymbhs
	 *            the ymbhs to set
	 */
	public void setYmbhs(String[] ymbhs) {
		this.ymbhs = ymbhs;
	}

	/**
	 * @toDo:get
	 * @return the urlFlag
	 */
	public String getUrlFlag() {
		return urlFlag;
	}

	/**
	 * @toDo:set
	 * @param urlFlag
	 *            the urlFlag to set
	 */
	public void setUrlFlag(String urlFlag) {
		this.urlFlag = urlFlag;
	}

	/**
	 * @toDo:get
	 * @return the rightTreeList
	 */
	public List<AppsysQxygns> getRightTreeList() {
		return rightTreeList;
	}

	/**
	 * @toDo:set
	 * @param rightTreeList
	 *            the rightTreeList to set
	 */
	public void setRightTreeList(List<AppsysQxygns> rightTreeList) {
		this.rightTreeList = rightTreeList;
	}

	/**
	 * @toDo:get
	 * @return the appsysYmgnBeanList
	 */
	public List<AppsysYmgnBean> getAppsysYmgnBeanList() {
		return appsysYmgnBeanList;
	}

	/**
	 * @toDo:set
	 * @param appsysYmgnBeanList
	 *            the appsysYmgnBeanList to set
	 */
	public void setAppsysYmgnBeanList(List<AppsysYmgnBean> appsysYmgnBeanList) {
		this.appsysYmgnBeanList = appsysYmgnBeanList;
	}

	/**
	 * @toDo:get
	 * @return the qxbh
	 */
	public Long getQxbh() {
		return qxbh;
	}

	/**
	 * @toDo:set
	 * @param qxbh
	 *            the qxbh to set
	 */
	public void setQxbh(Long qxbh) {
		this.qxbh = qxbh;
	}

	/**
	 * @toDo:get
	 * @return the gnbhs
	 */
	public String[] getGnbhs() {
		return gnbhs;
	}

	/**
	 * @toDo:set
	 * @param gnbhs
	 *            the gnbhs to set
	 */
	public void setGnbhs(String[] gnbhs) {
		this.gnbhs = gnbhs;
	}

	/**
	 * @toDo:get用户组业务层接口;
	 * @return the yhzService
	 */
	public YhzService getYhzService() {
		return yhzService;
	}

	/**
	 * @toDo:set用户组业务层接口;
	 * @param yhzService
	 *            the yhzService to set
	 */
	public void setYhzService(YhzService yhzService) {
		this.yhzService = yhzService;
	}

	/**
	 * @toDo:get用户组list;
	 * @return the yhzList
	 */
	public List<AppsysYhz> getYhzList() {
		return yhzList;
	}

	/**
	 * @toDo:set用户组list;
	 * @param yhzList
	 *            the yhzList to set
	 */
	public void setYhzList(List<AppsysYhz> yhzList) {
		this.yhzList = yhzList;
	}

	/**
	 * @toDo:get用户当前所属用户组list;
	 * @return the currYhzList
	 */
	public List<AppsysYhz> getCurrYhzList() {
		return currYhzList;
	}

	/**
	 * @toDo:set用户当前所属用户组list;
	 * @param currYhzList
	 *            the currYhzList to set
	 */
	public void setCurrYhzList(List<AppsysYhz> currYhzList) {
		this.currYhzList = currYhzList;
	}

	/**
	 * @toDo:get
	 * @return the yhzbhs
	 */
	public String getYhzbhs() {
		return yhzbhs;
	}

	/**
	 * @toDo:set
	 * @param yhzbhs
	 *            the yhzbhs to set
	 */
	public void setYhzbhs(String yhzbhs) {
		this.yhzbhs = yhzbhs;
	}

	/**
	 * @toDo:
	 * @return
	 */
	public Timestamp getTimeStamp() {
		return timeStamp;
	}

	/**
	 * @toDo:
	 * @param timeStamp
	 */
	public void setTimeStamp(Timestamp timeStamp) {
		Random ran = new Random();
		this.timeStamp = new Timestamp(ran.nextLong());
	}

	/**
	 * @return the yhbh
	 */
	public String getYhbh() {
		return yhbh;
	}

	/**
	 * @param yhbh
	 *            the yhbh to set
	 */
	public void setYhbh(String yhbh) {
		this.yhbh = yhbh;
	}

	/**
	 * toDo:get
	 * 
	 * @return xzzdList
	 */
	public List<AppsysYmgnBean> getXzzdList() {
		return xzzdList;
	}

	/**
	 * toDo:set
	 * 
	 * @param xzzdList
	 */
	public void setXzzdList(List<AppsysYmgnBean> xzzdList) {
		this.xzzdList = xzzdList;
	}

	/**
	 * toDo:get
	 * 
	 * @return fwjlList
	 */
	public List<AppsysYmxzfwjlBean> getFwjlList() {
		return fwjlList;
	}

	/**
	 * toDo:set
	 * 
	 * @param fwjlList
	 */
	public void setFwjlList(List<AppsysYmxzfwjlBean> fwjlList) {
		this.fwjlList = fwjlList;
	}

	/**
	 * toDo:get
	 * 
	 * @return qxfwjlgxs
	 */
	public List<AppsysQxfwjlgx> getQxfwjlgxs() {
		return qxfwjlgxs;
	}

	/**
	 * toDo:set
	 * 
	 * @param qxfwjlgxs
	 */
	public void setQxfwjlgxs(List<AppsysQxfwjlgx> qxfwjlgxs) {
		this.qxfwjlgxs = qxfwjlgxs;
	}

	/**
	 * toDo:get
	 * 
	 * @return ymxztjList
	 */
	public List<YmxztjBean> getYmxztjList() {
		return ymxztjList;
	}

	/**
	 * toDo:set
	 * 
	 * @param ymxztjList
	 */
	public void setYmxztjList(List<YmxztjBean> ymxztjList) {
		this.ymxztjList = ymxztjList;
	}

	/**
	 * toDo:get
	 * 
	 * @return xzbh
	 */
	public String getXzbh() {
		return xzbh;
	}

	/**
	 * toDo:set
	 * 
	 * @param xzbh
	 */
	public void setXzbh(String xzbh) {
		this.xzbh = xzbh;
	}

	/**
	 * @return the ryxm
	 */
	public String getRyxm() {
		return ryxm;
	}

	/**
	 * @param ryxm
	 *            the ryxm to set
	 */
	public void setRyxm(String ryxm) {
		this.ryxm = ryxm;
	}

	public QxglService getQxglService() {
		return qxglService;
	}

	public void setQxglService(QxglService qxglService) {
		this.qxglService = qxglService;
	}

	public QxygnsService getQxygnsService() {
		return qxygnsService;
	}

	public void setQxygnsService(QxygnsService qxygnsService) {
		this.qxygnsService = qxygnsService;
	}

	public List<AppsysQxygns> getMainFunc() {
		return mainFunc;
	}

	public void setMainFunc(List<AppsysQxygns> mainFunc) {
		this.mainFunc = mainFunc;
	}
	
	
}
